package com.sltx.controller;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sltx.controller.base.BaseController;
import com.sltx.util.PageData;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2023/8/10 9:53
 */

@Controller
@ResponseBody
public class portController extends BaseController {

    //推送json数据接口
    @RequestMapping("/data")
    public Object date(@RequestBody PageData pd) throws Exception {
        String json = JSON.toJSONString(pd);//转为string类型
        byte[] inputBytes = json.getBytes("UTF-8");
        // 加密
        byte[] encodedBytes = Base64.getEncoder().encode(inputBytes);
        String encodedString = new String(encodedBytes);
        //根据请求的URL地址创建URL对象。使用java.net.URL类的URL构造函数来创建URL对象，传入请求的URL地址作为参数
        String url = "http://112.26.28.28:8888/enterpriseData";
        URL obj = new URL(url);
        //创建URL对象后，我们需要打开连接。使用URL对象的openConnection()方法来打开连接，并将返回的连接对象强制转换为HttpURLConnection类。
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        //设置请求的方法为POST。使用连接对象的setRequestMethod()方法来设置请求的方法。
        conn.setRequestMethod("POST");
        //在发送POST请求时，通常需要设置一些请求头的信息，例如设置Content-Type为application/json。使用连接对象的setRequestProperty()方法来设置请求头的信息。
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("token", "eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2OTIxNTEzMTcsInNsdHhfcGFzc3dvcmQiOiIxcWF5MyEiLCJzbHR4X3VzZXIiOiJzbHR4In0.iMAt01KuA--K0T2b2ryDaNgp6eGkx1MrQjSPOmwjVXQ");

        conn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(encodedString);
        writer.flush();
        writer.close();
        //最后，我们可以通过连接对象的getInputStream()方法来获取服务器返回的响应内容。
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();

        while ((line = reader.readLine()) != null) {
            response.append(line);
        }

        reader.close();
        return response.append(line);
    }


}




